package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import pos.domain.*;
import pos.util.UIDGenerator;

public class JDBCCalendarioDAO implements ICalendarioDAO{


	private Connection conn;

    

    IDetallesDAO ddao;

    

    public JDBCCalendarioDAO() {
        conn = ConnectionManager.getInstance().checkOut();
        
        ddao = new JDBCDetallesDAO();
        
    }

    protected void finalize() {
        ConnectionManager.getInstance().checkIn(conn);
    }

    public void delete(String calendarioID) {

        String sql = "DELETE FROM calendario WHERE (calendarioid = ?) ";
        PreparedStatement stmt = null;

        String calendarioOID = selectOIDOfCalendario(calendarioID);

        if (calendarioOID!=null) {
        
        

        
        ddao.delete(conn, calendarioOID);

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, calendarioID);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
        }
    }

    

    private String selectOIDOfCalendario(String calendarioID) {
        PreparedStatement stmt = null;
        ResultSet result = null;
        String oid = null;
        String sql = "SELECT * FROM calendario WHERE (calendarioid = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, calendarioID);
            result = stmt.executeQuery();

            if (result.next()) {
                oid = result.getString("OID");
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return oid;
    }

    public List selectAllCalendarios() {
        PreparedStatement stmt = null;
        ResultSet result = null;
        List lo = new LinkedList();
        String sql = "SELECT * FROM calendario";

        try {
            stmt = conn.prepareStatement(sql);
            result = stmt.executeQuery();

            while (result.next()) {
                calendario o = new calendario();
                String calendarioOid = result.getString("OID");
                o.setIdCalendario(result.getString("orderid"));
                

                o.setDetalles(ddao.select(conn, calendarioOid));

                lo.add(o);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }

                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return lo;
    }

    public void insertCalendario(calendario o) {
        
        String oido = UIDGenerator.getInstance().getKey();
        PreparedStatement stmt = null;
        String sql = "INSERT INTO calendario ( OID, calendarioid) VALUES (?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, o.getIdCalendario());
           

            stmt.executeUpdate();

            
            ddao.insert(conn, o.getDetalles(), oido);

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
    }

     
	}


